#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

struct location {
	int val;
	int pos;
	vector<int>::iterator it;
};

void InitializePosition(vector<vector<int> > &, int, int);
string get_blocks(vector<int> &);

void find_location(vector<vector<int> > &, location &, location &);

int main() {
	int block_number;
	string action1, action2;
	int number1, number2, posa, posb;
	location loca, locb;
	vector<int>::iterator ita, itb;

	cin >> block_number;
	vector<vector<int> > block_array(block_number);
	for (int i = 0; i < block_array.size(); ++i)
		block_array[i].push_back(i);

	while (cin >> action1) {
		if (action1 == "quit")
			break;
		cin >> number1 >> action2 >> number2;
		if (number1 == number2)
			continue;
		loca.val = number1;
		locb.val = number2;
		find_location(block_array, loca, locb);
		if (loca.pos == locb.pos)
			continue;
		if (action1 == "move")
			InitializePosition(block_array, loca.pos, loca.val);
		if (action2 == "onto")
			InitializePosition(block_array, locb.pos, locb.val);

		block_array[locb.pos].insert(block_array[locb.pos].end(), loca.it,
				block_array[loca.pos].end());
		block_array[loca.pos].erase(loca.it, block_array[loca.pos].end());
	}

	for (int i = 0; i < block_array.size(); ++i) {
		cout << i << ":" << get_blocks(block_array[i]) << endl;
	}
	
#ifndef ONLINE_JUDGE
	system("pause");
#endif

	return 0;
}
void InitializePosition(vector<vector<int> > &block_array, int pos, int val) {
	for (vector<int>::reverse_iterator rit = block_array[pos].rbegin(); rit
			!= block_array[pos].rend(); ++rit) {
		int back_val = *rit;
		if (back_val == val)
			break;
		block_array[back_val].push_back(back_val);
		block_array[pos].pop_back();
	}
}
string get_blocks(vector<int> &block_vector) {
	stringstream nums;
	if (!block_vector.empty()) {
		for (vector<int>::iterator it = block_vector.begin(); it
				!= block_vector.end(); ++it)
			nums << ' ' << *it;
	}
	return nums.str();
}
void find_location(vector<vector<int> > &block_array, location &loca,
		location &locb) {
	loca.pos = -1;
	locb.pos = -1;
	for (int i = 0; i < block_array.size(); ++i) {
		if (!block_array[i].empty())
			for (vector<int>::iterator it = block_array[i].begin(); it
					!= block_array[i].end(); ++it) {
				if (*it == loca.val) {
					loca.pos = i;
					loca.it = it;
				}
				if (*it == locb.val) {
					locb.pos = i;
					locb.it = it;
				}
				if ((loca.pos != -1) && (locb.pos != -1) && (loca.pos
						== locb.pos))
					return;
			}
	}
}
